package com.unitechs.trap.excel.webapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.unitechs.trap.common.restful.JsonResult;
import com.unitechs.trap.excel.service.IAllDataService;
import com.unitechs.trap.excel.service.IJsonTestService;
import com.unitechs.trap.excel.service.IWranglingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "Excel表格整理")
@RequestMapping("/excel")
public class AllDataController {

    @Autowired
    private IAllDataService allDataService;
    @Autowired
    private IJsonTestService jsonTestService;
    @Autowired
    private IWranglingService wranglingService;

    @PostMapping("/insert/allData")
    @ApiOperation("写入all_data")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "url",name = "url",example = "F:\\A.xls"),
            @ApiImplicitParam(value = "coName",name = "coName",example = "AL"),
            @ApiImplicitParam(value = "version",name = "version",example = "A")
    })
    @CrossOrigin
    public String insertAllData(String url,String coName,String version){
        allDataService.insertAllData(url, coName,version);
        return "ok";
    }

    @PostMapping("/insert/varData")
    @ApiOperation("写入var_data")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "url",name = "url",example = "F:\\test.xls"),
            @ApiImplicitParam(value = "coName",name = "coName",example = "AL"),
            @ApiImplicitParam(value = "version",name = "version",example = "A")
    })
    public String insertVarData(String url,String coName,String version){
        allDataService.insertVarData(url,coName,version);
        return "ok";
    }

    @GetMapping("/read/excel/trap/json")
    @ApiOperation("读取出告警测试的json字符串")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "文件路径", name = "url", example = "G:\\trap规则整理\\数据转JSON测试"),
            @ApiImplicitParam(value = "设备IP", name = "devicesIp", example = "210.78.3.112")
    })
    public List<JSONObject> readTrapJson(String url, String devicesIp){
        List<JSONObject> jsonObjects = jsonTestService.writeJsonToExcel(url, devicesIp);
        return jsonObjects;
    }

    @GetMapping("/read/excel/to/test")
    @ApiOperation("接口测试")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "文件路径",name = "url",example = "G:\\trap规则整理\\数据转JSON测试"),
            @ApiImplicitParam(value = "设备号",name = "devicesIp",example = "210.78.3.112"),
            @ApiImplicitParam(value = "接口路径",name = "intface",example = "http://192.168.212.20:7007/api/ala/traprule/analysistest"),
            @ApiImplicitParam(value = "token值",name = "token",example = "web_eyJhbGciOiJIUzUxMiJ9.eyJzdWI " +
                    "iOiIxMDY3OTU1IiwibmV0VXNlcmlkIjoiemhvdXl6IiwidXNlck5hbWUiOiLlkajnjonmm4wiLCJleHAiOjE2Nzk2M" +
                    "jE1NDJ9.gi5kRDnPA5Bo1s7jb1rTiqrTJ2_aVnZOyesbDQWFls6XPeNU8nzvmUU4F5L0ueH6ByTmETcaDmBmajRrc4-pMg")

    })
    public List<JSONObject> testTrapByJson(String url,String devicesIp,String intface,String token){
        List<JSONObject> list = jsonTestService.testTrapJson(url, devicesIp, intface, token);
        return list;
    }

    @GetMapping("/all/data/wrangling")
    @ApiOperation("数据整理")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "规整后的文件路径及名称",name = "url",example = ""),
            @ApiImplicitParam(value = "数据存放的文件夹路径",name = "oldUrl",example = "")
    })
    public String wranglingData(String url,String oldUrl){
        wranglingService.wranglingDataFromExcel(url, oldUrl);
        return JsonResult.ok().getMessage();
    }

    @GetMapping("/all/data/wrangling/end")
    @ApiOperation("整理后的数据进行规整")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "接口响应的数据路径",name = "urlFrom",example = "G:\\trap规则整理\\数据转JSON测试"),
            @ApiImplicitParam(value = "各个厂商的数据路径",name = "urlTo",example = "G:\\trap规则整理\\新trap数据整理后")

    })
    public JsonResult wranglingDataEnd(String urlFrom,String urlTo){
        wranglingService.wranglingDataToExcel(urlFrom, urlTo);
        return JsonResult.ok();
    }

    @GetMapping("/message/gettrapoid")
    @ApiOperation("根据报文获取trapOid")
    @ApiImplicitParam(value = "文件路径",name = "url",example = "G:\\trap规则整理\\根据报文获取TrapOid")
    public JsonResult getTrapOidByMessage(String url){
        wranglingService.getTrapOidByMessage(url);
        return JsonResult.ok();
    }

}
